// pages/home/pages/party4Detail/party4Detail.js
const { toPost, toGet } = require('../../../../utils/request.js')
var r = require('../../../../utils/request.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    type:'',//0是书记信箱

    message:{},

    list:[],
    distanceHeight:0,

    speck:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type,options.id)
    let url = ''
    if(options.type == 0){
      url = '/secretaryLetter/detail'
    }else{
      url = '/massesLetter/detail'
    }
    //获取数据列表
    r.toGet(url, {letterId:options.id}, (res) => {
      // console.log(res.retData)
      this.setData({
        message:res.retData,
        type:options.type,
        list:res.retData.comments,
        id:options.id
      })
    })
  },

  // 获取值
  getval(e){
    // console.log(e.detail.value)
    this.setData({
      speck:e.detail.value
    })
  },

  // 发送
  submit(){
    var that = this
    if(!that.data.speck){
      wx.showToast({
        title: '评论不能为空！',
        icon:'none'
      })
      return
    }
    let url
    let param
    if (this.data.type == 0) {
      url = '/secretaryLetter/addComment'
      param ={
        letterId:that.data.id,commentContent:that.data.speck
      }
    } else{
      url = '/massesLetter/addComment'
      param ={
        letterId:that.data.id,userId: wx.getStorageSync('userId'),content:that.data.speck
      }
    }
    r.toPost(url, param, (res) => {
      // console.log(res.retData)
      wx.showToast({
        title: '已评论',
        icon:'none'
      })
      let url2
      if (this.data.type == 0) {
        url2 = '/secretaryLetter/detail'
      } else{
        url2 = '/massesLetter/detail'
      }
      
      r.toGet(url2, {letterId:that.data.id}, (res) => {
        // console.log(res.retData)
        that.setData({
          message:res.retData,
          list:res.retData.comments,
          speck:''
        })
      })
    })
  },

  // // 聚焦
  // focus(){
  //   var that = this
  //   console.log(1)
  //   wx.onKeyboardHeightChange(res => {
  //     console.log(res.height * 2,'11')
  //     that.setData({
  //       distanceHeight:res.height * 2
  //     })
  //   })
  // },
  // // 失焦
  // blur(){
  //   console.log(2)
  //   var that = this
  //   that.setData({
  //     distanceHeight:0
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})